package com.test;
/*
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;

class Person {
    private String name;
    private int age;

    public Person(String name, int age) {
        this.setName(name);
        this.setAge(age);
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public String toString() {
        return "姓名：" + this.name + ",年龄：" + this.age;
    }
}

public class Example02 {
    public static void main(String[] args) {
        Class<?> c = null;
        try {
            c = Class.forName("com.test.Person");
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
        //获取全部方法,存储到Method类数组对象 m 中
        Method m[] = c.getMethods();
        for (int i = 0; i < m.length; i++) {        //遍历数组m循环输出方法信息
            Class<?> r = m[i].getReturnType();    //获取方法的返回值类型
            Class<?> p[] = m[i].getParameterTypes();   //获取全部的参数类型
            int xx = m[i].getModifiers();  //获取方法的权限修饰符
            System.out.print(Modifier.toString(xx) + " ");  //还原修饰符
            System.out.print(r.getName() + " ");      //获取方法名称
            System.out.print(m[i].getName());
            System.out.print("(");
            for(int x=0; x< p.length;x++){
                System.out.print(p[x].getName() + " "+"args"+" ");
                if(x<p.length-1){
                    System.out.print(",");
                }
            }
            //获取方法抛出的全部异常
            Class<?> ex[] =m[i].getExceptionTypes();
            if(ex.length>0){
                System.out.print(") throws ");
            }else{
                System.out.print(")");
            }
            for(int j=0;i<ex.length;j++){
                System.out.print(ex[j].getName());    //输出异常信息
                if(j<ex.length-1){
                    System.out.print(",");
                }
            }
            System.out.println();
        }
    }
}


 */


/*
import java.lang.reflect.Field;
import java.lang.reflect.Modifier;

class Person {
    private String name;
    private int age;

    public Person(String name, int age) {
        this.setName(name);
        this.setAge(age);
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public String toString() {
        return "姓名：" + this.name + ",年龄：" + this.age;
    }
}

public class Example02 {
    public static void main(String[] args) {
        Class<?> c1 = null;
        try {
            c1 = Class.forName("com.test.Person");
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
        {
            //获取本类属性，存储到Field类数组f当中
            Field f[] = c1.getDeclaredFields();
            for (int i = 0; i < f.length; i++) {            //循环输出属性信息
                Class<?> r = f[i].getType();           //获取属性的类型
                int mo = f[i].getModifiers();          //获取属性权限修饰符
                String priv = Modifier.toString(mo);  //转换属性的权限修饰符
                System.out.print("本类属性:");
                System.out.print(priv + " ");             //输出属性权限修饰符
                System.out.print(r.getName() + " ");     //输出属性类型
                System.out.print(f[i].getName());      //输出属性名称
                System.out.println(";");
            }
        }
    }
}

 */


/*
import java.lang.reflect.Method;

class Person {
    private String name;
    private int age;

    public Person() {
    }

    public Person(String name) {
        this.name = name;
    }

    public Person(String name, int age) {
        this.setName(name);
        this.setAge(age);
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public void sayHello() {
        System.out.println("Hello World!");
    }

    public String toString() {
        return "姓名：" + this.name + ",年龄：" + this.age;
    }
}

public class Example02 {
    public static void main(String args[]) {
        Class<?> c = null;
        try {
            c = Class.forName("com.test.Person");
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
        try {
            Method met = c.getMethod("sayHello");
            met.invoke(c.newInstance());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

 */


/*
import java.lang.reflect.Method;

class Person {
    private String name;
    private int age;

    public Person() {
    }

    public Person(String name) {
        this.name = name;
    }

    public Person(String name, int age) {
        this.setName(name);
        this.setAge(age);
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public String toString() {
        return "姓名：" + this.name + ",年龄：" + this.age;
    }
}
public class Example02 {
    public static void main(String[] args){
        Class<?> c = null;
        Object obj = null;
        try{
            c = Class.forName("com.test.Person");	//对象c为Class类型
        }catch(ClassNotFoundException e){
            e.printStackTrace();
        }
        try{
            obj = c.newInstance();					//实例化Class对象
        }catch(InstantiationException e){
            e.printStackTrace();
        }catch(IllegalAccessException e){
            e.printStackTrace();
        }
        setter(obj,"name","张三",String.class);
        setter(obj,"age",18,int.class);
        System.out.print("姓名：");
        getter(obj,"name");
        System.out.print("年龄：");
        getter(obj,"age");
    }
    public static void setter(Object obj,String att,Object value,Class<?>
            type){
        try {
            Method met= obj.getClass().getMethod("set"+initStr(att),type);
            met.invoke(obj,value);
        }catch(Exception e){
            e.printStackTrace();
        }
    }
    public static void getter(Object obj,String att){
        try {
            Method met= obj.getClass().getMethod("get"+initStr(att));
            System.out.println(met.invoke(obj));
        }catch(Exception e){
            e.printStackTrace();
        }
    }
    public static String initStr(String old){
        String str= old.substring(0,1).toUpperCase()+old.substring(1);
        return str;
    }
}

 */

import java.lang.reflect.Field;

class Person {
    private String name;
    private int age;
    public Person(){}
    public Person(String name){
        this.name = name;
    }
    public Person(String name,int age){
        this.setName(name);
        this.setAge(age);
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public int getAge() {
        return age;
    }
    public void setAge(int age) {
        this.age = age;
    }
    public void sayHello(){
        System.out.println("Hello World!");
    }
    public String toString() {
        return "姓名："+this.name+",年龄："+this.age;
    }
}

public class Example02 {
    public static void main(String[] args) throws Exception{
        Class<?> c = null;                             //声明一个Class对象
        Object obj = null;                             //声明一个Object对象
        c = Class.forName("com.test.Person");   //实例化Class对象
        obj = c.newInstance();                         //实例化Object对象
        Field nameField = null;                        //表示name属性
        Field ageField = null;                         //表示age属性
        nameField = c.getDeclaredField("name");     //获取name属性
        ageField = c.getDeclaredField("age");        //获取age属性
        nameField.setAccessible(true);       //将name属性设置为可被外部访问
        nameField.set(obj,"张三");             //设置name属性值
        ageField.setAccessible(true);         //将age属性设置为可被外部访问
        ageField.set(obj,30);                   //设置age属性值
        System.out.println("姓名："+nameField.get(obj));
        System.out.println("年龄："+ageField.get(obj));
    }
}